package goleetcode

func distMoney(money int, children int) int {
	money -= children
	if money < 0 {
		return -1
	}
	if money < 7 {
		return 0
	}
	ans := 0
	for i := 0; i <= children; i++ {
		if i == children && money == 3 {
			ans--
			money = 0
		} else {
			if money-7 >= 0 {
				ans += 1
				money -= 7
			} else {
				money = 0
				break
			}
		}
	}
	if money > 0 {
		ans--
	}
	return ans
}

func distMoney2(money , children int) int {
	if money < children {
		return -1
	}
	// give every child one dollor
	money -= children
	// then you need to give more 7 dollor to children (plus 1 dollor equals to 8 dollor)
	cnt := min(money /7, children)
	money -= cnt * 7
	children -= cnt

	if(children == 0 && money > 0 || (children == 1 && money == 3)) {
		cnt -= 1
	}
	return cnt
}